package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mybatisplus.model.domain.Dot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.BuildingService;
import com.example.mybatisplus.model.domain.Building;
import java.util.List;

/**
 *
 *  前端控制器
 *
 *
 * @author zzy
 * @since 2024-06-24
 * @version v1.0
 */
@RestController
@RequestMapping("/api/building")
public class BuildingController {
    @Autowired
    private BuildingService buildingService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Building  building =  buildingService.getById(id);
        return JsonResponse.success(building);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        buildingService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateBuilding(Building  building) throws Exception {
        buildingService.updateById(building);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Building
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Building  building) throws Exception {
        buildingService.save(building);
        return JsonResponse.success(null);
    }

    @PostMapping("/building_add")
    public JsonResponse building_add(@RequestBody Building building){
        building.setIsDeleted(0);
        LambdaQueryWrapper<Building> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Building::getType, building.getType()).eq(Building::getName, building.getName());
        Building building1 = buildingService.getOne(wrapper);
        if(building1.getIsDeleted() == 0){
            return JsonResponse.failure(String.format("Building \"%s\" has already existed.", building.getName()));
        }
        else {
            Building building2 = new Building();
            building2.setId(building1.getId());
            building2.setIsDeleted(0);
            buildingService.updateById(building2);
        }
        buildingService.save(building);
        return JsonResponse.success(true, "Building adding success.");
    }
    @DeleteMapping("/building_delete")
    public JsonResponse building_delete(Long buildingID){
        Building building = new Building().setId(buildingID).setIsDeleted(1);
        Boolean b = buildingService.updateById(building);
        return JsonResponse.success(b, "Building delete success.");
    }
    @PostMapping("/building_modify")
    public JsonResponse building_modify(@RequestBody Building building){
        Boolean b = buildingService.updateById(building);
        return JsonResponse.success(b, "Building modify success.");
    }
    @PostMapping ("/select_building_by_buildingname")
    public JsonResponse select_building_by_buildingname(@RequestBody Dot dot){
        List<Building> buildingList = buildingService.selectBuildByNamePage(dot.getData().toString(), dot);
        return JsonResponse.success(buildingList, "Select Building Success.");
    }
    @GetMapping("/building_all")
    public JsonResponse  building_all(@RequestBody Dot dot){
        List<Building> buildingList = buildingService.selectBuildByNamePage("", dot);
        return JsonResponse.success(buildingList, "Select All buildings success.");
    }
    @PostMapping("/get_total_people_by_BuildingID")
    public JsonResponse get_total_people_by_BuildingID(long BuildingID){
        int id = buildingService.getTotelByBuildingID(BuildingID);
        return JsonResponse.success(id, "Get totel building number success.");
    }
    @PostMapping ("/building_by_gender")
    public JsonResponse building_by_gender(@RequestBody Dot dot){
        List<Building> buildingList = buildingService.selectBuildByGenderPage(dot);
        return JsonResponse.success(buildingList, "Select Building Success.");
    }
    @PostMapping ("/building_by_area")
    public JsonResponse building_by_area(@RequestBody Dot dot){
        List<Building> buildingList = buildingService.selectBuildByAreaPage(dot);
        return JsonResponse.success(buildingList, "Select Building Success.");
    }
}

